#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 22 avr. 2011

@author: nathanael
'''
import unittest
from lxml import etree
import StringIO

from pybible.core import data

def get_xml_test():
    return StringIO.StringIO("<root attr='val'>"
                             "<part_1><sub_part_11/><sub_part_12/></part_1>"
                             "<part_2><sub_part_21/><sub_part_22/></part_2>"
                             "</root>")    
def get_book_test():
    return StringIO.StringIO("<book>"
                             "<header>"
                             "<key type='author' value='herge'/>"
                             "<key type='title' value='tintin'/>"
                             "<key type='author' value='moi'/>"
                             "</header>"
                             "<part>"
                             "</part>"
                             "</book>")
    
    

class TestXMLData(unittest.TestCase):
    def setUp(self):
        self.part = data._loadXMLDATA(get_xml_test())

    def tearDown(self):
        pass


    def test_tag(self):
        self.assertEqual(self.part.tag, 'root')
    def test_attrs(self):
        self.assertEqual(self.part.attrs.get('attr'), 'val')
    def test_build_child(self):
        self.assertEqual(self.part._build_child(None), None)
        c = self.part._build_child(self.part._element[0])
        self.assertEqual(c.tag, 'part_1')
        self.assertEqual(type(c), type(self.part))
        self.assertEqual(c._root, self.part._root)
    def test_max_deep(self):
        self.assertEqual(self.part.maxdeep(), 3)
        c = self.part._build_child(self.part._element[0])
        self.assertEqual(c.maxdeep(), 2)
    def test_my_deep(self):
        self.assertEqual(self.part.mydeep(), 1)
        c = self.part._build_child(self.part._element[0])
        self.assertEqual(c.mydeep(), 2)
    def test_get_xpath(self):
        self.assertEqual(self.part.get_xpath(), "/root")    
        c = self.part._build_child(self.part._element[0])
        self.assertEqual(c.get_xpath(), "/root/part_1")
        c = self.part._build_child(self.part._element[1][0])
        self.assertEqual(c.get_xpath(), "/root/part_2/sub_part_21")
    def test_eval_xpath(self):
        evaluated = self.part.eval_xpath('/root')
        self.assertEqual(len(evaluated), 1)
        evaluated = evaluated[0]
        self.assertEqual(self.part, evaluated)
        
        evaluated = self.part.eval_xpath('/root/part_2')
        self.assertEqual(len(evaluated), 1)
        evaluated = evaluated[0]
        self.assertEqual(self.part._build_child(self.part._element[1]),
                         evaluated)
        
        evaluated = evaluated.eval_xpath('sub_part_21', relatif=True)
        self.assertEqual(len(evaluated), 1)
        evaluated = evaluated[0]
        self.assertEqual(self.part._build_child(self.part._element[1][0]),
                         evaluated)
    def test_iter(self):
        r = ['root', 'part_1', 'sub_part_11', 'sub_part_12',
             'part_2', 'sub_part_21', 'sub_part_22']
        i = 0
        for e in self.part.iter():
            self.assertEqual(e.tag, r[i])
            i += 1
        
        r = ['part_1', 'part_2']
        i = 0
        for e in self.part:
            self.assertEqual(e.tag, r[i])
            i += 1
    def test_get_parent(self):
        self.assertEqual(self.part.get_parent(), None)
        c = self.part._build_child(self.part._element[0])
        self.assertEqual(c.get_parent(), self.part)
    def test_get_next(self):
        self.assertEqual(self.part.get_next(), None)
        c = self.part._build_child(self.part._element[0])
        self.assertEqual(c.get_next().tag, 'part_2')





class TestBookPart(unittest.TestCase):
    def setUp(self):
        self.part = data.loadBookPart(get_xml_test())
    def test_get_next(self):
        self.assertEqual(self.part.get_next(), None)
        c = self.part._build_child(self.part._data.eval_xpath('/root/part_1/sub_part_11')[0])
        self.assertEqual(c.part_name_type, 'sub_part_11')
        c = c.get_next()
        self.assertEqual(c.part_name_type, 'sub_part_12')
        c = c.get_next()
        self.assertEqual(c, None)
        
        c = self.part._build_child(self.part._data.eval_xpath('/root/part_1/sub_part_11')[0])
        self.assertEqual(c.part_name_type, 'sub_part_11')
        c = c.get_next(flat=True)
        self.assertEqual(c.part_name_type, 'sub_part_12')
        c = c.get_next(flat=True)
        self.assertEqual(c.part_name_type, 'sub_part_21')
    def test_get_previsous(self):
        self.assertEqual(self.part.get_previous(), None)
        c = self.part._build_child(self.part._data.eval_xpath('/root/part_2/sub_part_22')[0])
        self.assertEqual(c.part_name_type, 'sub_part_22')
        c = c.get_previous()
        self.assertEqual(c.part_name_type, 'sub_part_21')
        c = c.get_previous()
        self.assertEqual(c, None)
        
        c = self.part._build_child(self.part._data.eval_xpath('/root/part_2/sub_part_22')[0])
        self.assertEqual(c.part_name_type, 'sub_part_22')
        c = c.get_previous(flat=True)
        self.assertEqual(c.part_name_type, 'sub_part_21')
        c = c.get_previous(flat=True)
        self.assertEqual(c.part_name_type, 'sub_part_12')
        


class TestsBook(unittest.TestCase):
    def setUp(self):
        self.book = data.loadBook(get_book_test())
    
    def test_get_part(self):
        self.assertNotEqual(self.book.get_part(), None)
        self.assertEqual(self.book.get_part()._data.get_xpath(), '/book/part')
    
    
    def test_get_header(self):
        self.assertNotEqual(self.book.get_header(), None)
        self.assertEqual(self.book.get_header()._data.get_xpath(), '/book/header')
        

class TestsBookHeader(unittest.TestCase):
    def setUp(self):
        self.header = data.loadBook(get_book_test()).get_header()
        
    def test_getitem(self):
        self.assertEqual(self.header['title'], 'tintin')
        self.assertEqual(self.header['author'], ['herge', 'moi'])
        self.assertEqual(self.header['language'], None)
        
    def test_keys_tools(self):
        self.assertEqual(self.header.get_valideKey(),
                         ('type', 'topic', 'author', 'title',
                          'language', 'translation'))
        
        self.assertTrue(self.header.isAtomicKey('title'))
        self.assertFalse(self.header.isAtomicKey('author'))
        
        self.assertFalse(self.header.isMultiKey('title'))
        self.assertTrue(self.header.isMultiKey('author'))
        
        
        
    
    
        
if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
